<html>
<head>
<title>DreamCMS ~ Install</title>
<style type="text/css">
<!--
html, body {
	margin-top:15px;
	background: #000;
	font-family: Verdana, Arial, Helvetica, sans-serif;
	font-size:0.85em;
	color:#a7a5a4;
	text-align:center;
}

a {
 color:#a7a5a4;
}
-->
</style>
</head>
<body>
<p><img src="dream-badge.png"></p>
<?php
	/*
		DreamCMS Version 1.0
		Developed by: Andrei Mikalov (Zeus)
		
		Thanks to Adam Davis
	*/

	//  Primitive installer
	
	
	require_once("../models/settings.php");
	
	//Dbal Support - Thanks phpBB ; )
	require_once("../models/db/".$dbtype.".php");
	require_once("../models/funcs.user.php");
	
	//Construct a db instance
	$db = new $sql_db();
	if(is_array($db->sql_connect(
							$db_host, 
							$db_user,
							$db_pass,
							$db_name, 
							$db_port,
							false, 
							false
	))) {
		echo "<strong>Unable to connect to the database, check your settings.</strong>";	
		
		echo "<p><a href=\"?install=true\">Try again</a></p>";
	}
	else
	{
	
	if(returns_result("SELECT * FROM ".$db_table_prefix."Groups LIMIT 1") > 0)
	{
		echo "<strong>DreamCMS has already been installed.<br /> Please remove or rename the install directory.</strong>";	
	}
	else
	{
		if(isset($_GET["install"]))
		{
	
				$db_issue = false;
			
				$groups_sql = "
					CREATE TABLE IF NOT EXISTS `".$db_table_prefix."Groups` (
					`Group_ID` int(11) NOT NULL auto_increment,
					`Group_Name` varchar(225) NOT NULL,
					 PRIMARY KEY  (`Group_ID`)
					) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
				";
				
				$group_entry = "
					INSERT INTO `".$db_table_prefix."Groups` (`Group_ID`, `Group_Name`) VALUES
					(1, 'Standard User');
				";
				
				$users_sql = "
					 CREATE TABLE IF NOT EXISTS `".$db_table_prefix."Users` (
					`User_ID` int(11) NOT NULL auto_increment,
					`Username` varchar(150) NOT NULL,
					`Username_Clean` varchar(150) NOT NULL,
					`Password` varchar(225) NOT NULL,
					`Email` varchar(150) NOT NULL,
					`ActivationToken` varchar(225) NOT NULL,
					`LastActivationRequest` int(11) NOT NULL,
					`LostPasswordRequest` int(1) NOT NULL default '0',
					`Active` int(1) NOT NULL,
					`Group_ID` int(11) NOT NULL,
					`SignUpDate` int(11) NOT NULL,
					 `LastSignIn` int(11) NOT NULL,
					 PRIMARY KEY  (`User_ID`)
					) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
				";
				
				$news_sql = "
				CREATE TABLE IF NOT EXISTS `".$db_table_prefix."News` (
                     `id` int(11) NOT NULL AUTO_INCREMENT,
                     `title` text COLLATE latin1_general_ci NOT NULL,
                     `content` text COLLATE latin1_general_ci NOT NULL,
                     `author` text COLLATE latin1_general_ci NOT NULL,
                     `date` date NOT NULL DEFAULT '0000-00-00',
                     PRIMARY KEY (`id`)
                     ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=11 ;
					 ";

                $news_entry = "
INSERT INTO `DreamCMS_News` (`id`, `title`, `content`, `author`, `date`) VALUES
(1, 'Thanks for downloading', '            <p>Thank you for downloading DreamCMS. 100% Free and Opensource. <br>\r\n    This is based on UserCake.</p>\r\n            \r\n            <p>Copyright (c) 2011 <a href=http://l0rd.me>Andrei Mikalov</a> and <a href=http://adamdavis.co.uk>Adam Davis</a></p>\r\n            \r\n            <p>Permission is hereby granted, free of charge, to any person obtaining a copy\r\n            of this software and associated documentation files (the Software), to deal\r\n            in the Software without restriction, including without limitation the rights\r\n            to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\r\n            copies of the Software, and to permit persons to whom the Software is\r\n            furnished to do so, subject to the following conditions:</p>\r\n            \r\n            <p>The above copyright notice and this permission notice shall be included in\r\n            all copies or substantial portions of the Software.</p>\r\n            \r\n            <p>THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r\n            IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\r\n            FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\r\n            AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\r\n            LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\r\n            OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\r\n    THE SOFTWARE.</p>', 'Andrei Zeus Mikalov', '2011-09-11'),
(2, 'Thanks to..', 'Jonas Laursen and Craig Sledmore for helping me with this news script.', 'Andrei Zeus Mikalov', '2011-09-11');
";
				
				
				if($db->sql_query($groups_sql))
				{
					echo "<p>".$db_table_prefix."Groups table created.....</p>";
				}
				else
				{
					echo "<p>Error constructing ".$db_table_prefix."Groups table.</p><br /><br /> DBMS said: ";
					
					echo print_r($db->_sql_error());
					$db_issue = true;
				}
				
				if($db->sql_query($group_entry))
				{
					echo "<p>Inserted Standard User group into ".$db_table_prefix."Groups table.....</p>";
				}
				else
				{
					echo "<p>Error constructing Groups table.</p><br /><br /> DBMS said: ";
					
					echo print_r($db->_sql_error());
					$db_issue = true;
				}
				
				if($db->sql_query($users_sql))
				{
					echo "<p>".$db_table_prefix."Users table created.....</p>";
				}
				else
				{
					echo "<p>Error constructing user table.</p><br /><br /> DBMS said: ";
					
					echo print_r($db->_sql_error());
					$db_issue = true;
				}
				
				if($db->sql_query($news_sql))
				{
					echo "<p>".$db_table_prefix."News table created.....</p>";
				}
				else
				{
					echo "<p>Error constructing ".$db_table_prefix."News table.</p><br /><br /> DBMS said: ";
					
					echo print_r($db->_sql_error());
					$db_issue = true;
				}
				
				if($db->sql_query($news_entry))
				{
					echo "<p>Inserted Standard News into ".$db_table_prefix."News table.....</p>";
				}
				else
				{
					echo "<p>Error constructing News table.</p><br /><br /> DBMS said: ";
					
					echo print_r($db->_sql_error());
					$db_issue = true;
				}
				
				
				if(!$db_issue)
				echo "<p><strong>Database setup complete, please delete the install folder.</strong></p>";
				else
				echo "<p><a href=\"?install=true\">Try again</a></p>";
				
			
				
		}
		else
		{
	?>
			<a href="?install=true">Install DreamCMS</a>
	
	
	<?php } } }
	?>
</body>
</html>